<?php

namespace Drupal\shushengyuan\Schemas;

use Drupal\shusy_promote\Entity\ShusyPromote;
use Drupal\shusy_store\Entity\ShusyStore;
use Drupal\shusy_team\Entity\ShusyTeam;
use Drupal\shusy_team_apply\Entity\ShusyTeamApply;
use Drupal\wx_mini_qr\Entity\WxMiniQr;

/**
 * @OA\Schema(
 *   schema="Login",
 *   type="object",
 *   description="登录状态信息,是否显示商家入驻等控制信息均使用此传递"
 * )
 */
class Login extends AbstractBaseModel {

  protected function getEntityType() {
    return 'user';
  }

  protected function setValue() {
    [$pid, $qr, $scene] = $this->context;
    watchdog(__CLASS__, serialize($this->context));
    $storeSettle = FALSE;
    $teamApply = FALSE;
    $errandApply = FALSE;
    $type = 'none';
    $typeArgs = [];
    if (!empty($scene)) {
      if (module_exists('wx_mini_qr')) {
        $scene = (int) $scene;
        $miniQr = entity_load_single('wx_mini_qr', $scene);
        if ($miniQr instanceof WxMiniQr) {
          $value = unserialize($miniQr->value);

          if ($miniQr->module == 'shusy_team') {
            //扫统一申请码
            if ($value == 'teamApply') {
              $teamApply = TRUE;
            }
            //扫成员推广码
            if (!empty(strstr($miniQr->name, "teamApply_"))) {
              [, $pid] = explode('teamApply_', $miniQr->name);
              $teamApply = TRUE;
            }
            //扫店铺推广码
            if (!empty(strstr($miniQr->name, "storeApply_"))) {
              [, $pid] = explode('storeApply_', $miniQr->name);
              $storeSettle = TRUE;
            }
          }

          if ($miniQr->module == 'shushengyuan') {
            if (!empty(strstr($miniQr->name, "share_product_"))) {
              $type = 'product';
              $typeArgs = [
                'pid' => $value['product'],
              ];
            }

            if (!empty(strstr($miniQr->name, "share_qr_"))) {
              [, $pid] = explode('share_qr_', $miniQr->name);
            }

          }

          if ($miniQr->module == 'shusy_errand') {
            //扫统一申请码
            if ($value == 'errandApply') {
              $errandApply = TRUE;
            }
            //扫成员推广码
            if (!empty(strstr($miniQr->name, "errandApply_"))) {
              [, $pid] = explode('errandApply_', $miniQr->name);
              $errandApply = TRUE;
            }
          }

        }
      }
    }
    //普通推广也显示商家入驻
    $es = shusy_promote_load_multiple(FALSE, ['uid' => $pid]);
    if (!empty($es)) {
      $e = reset($es);
      if ($e instanceof ShusyPromote && $e->status == 2) {
        //状态 0 未申请 1 审核中 2 已通过 3 未通过
        $storeSettle = TRUE;
      }
    }

    //如果推广者是商家 则显示商家入驻
    $es = shusy_store_load_multiple(FALSE, ['uid' => $pid]);
    if (!empty($es)) {
      $e = reset($es);
      if ($e instanceof ShusyStore && $e->status == 1) {
        $storeSettle = TRUE;
      }
    }

    //如果是内部推广人员 则显示商家入驻
    $teams = shusy_team_apply_load_multiple(FALSE, [
      'uid' => $pid,
    ]);
    if (!empty($teams)) {
      $team = reset($teams);
      if ($team instanceof ShusyTeamApply && $team->status == 1) {
        $storeSettle = TRUE;
      }
    }
    if ($this->isDev()) {
      $storeSettle = TRUE;
      $teamApply = TRUE;
      $errandApply = TRUE;
    }
    $data = [
      //是否显示商家入驻
      'storeSettle' => $storeSettle,
      //显示内部推广申请入口
      'teamApply' => $teamApply,
      //跑腿申请
      'errandApply' => $errandApply,
    ];
    $cid = '__login__' . $this->entity_id;
    $cache = $this->cacheGet($cid);
    if (!empty($cache)) {
      $data = $cache->data;
    }
    //缓存一段时间打开入口
    $this->cacheSet($cid, [
      //是否显示商家入驻
      'storeSettle' => $storeSettle || $data['storeSettle'],
      //显示内部推广申请入口
      'teamApply' => $teamApply || $data['teamApply'],
      //跑腿申请
      'errandApply' => $errandApply || $data['errandApply'],
    ], REQUEST_TIME + (7 * 24 * 60 * 60));
    $value = [
      //是否显示商家入驻
      'storeSettle' => $storeSettle,
      //显示内部推广申请入口
      'teamApply' => $teamApply,
      //跑腿申请
      'errandApply' => $errandApply,
      /**
       * @OA\Property(
       *   property="type",
       *   type="string",
       *   description="跳转类型 none 不跳转 product 产品详情",
       * ),
       */
      'type' => $type,
      /**
       * @OA\Property(
       *   property="typeArgs",
       *   description="参数 不同的跳转类型 参数不一样 具体情况具体查看文档",
       *   ref="#/components/schemas/TypeArgs"
       * )
       */
      'typeArgs' => $typeArgs,
    ];
    $this->merge($value);
  }

}
